package com.yulongtian.algorithms.binarysearch01;

/**
 * https://leetcode.cn/problems/guess-number-higher-or-lower/
 * 可
 *
 * @author yulongTian
 * @create 2023-01-31 13:38
 */

/**
 * 二分查找入门
 */
public class Test01 {
    public static void main(String[] args) {
        System.out.println(guessNumber(2));
    }


    public static int guessNumber(int n) {
        int ans = 1;
        int left = 1;
        int right = n;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            int temp = guess(mid);
            if (temp == 0) {
                ans = mid;
                break;
            }
            if (temp == 1) {
                left = mid + 1;
                continue;
            }
            if (temp == -1) {
                right = mid - 1;
            }
        }
        return ans;
    }

    private static int guess(int mid) {
        int num = 2;
        return Integer.compare(num, mid);
    }
}
